package com.tms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tms.domain.RepairApp;
import com.tms.domain.User;
import com.tms.mapper.develop.RepairRecordMapper;
import com.tms.service.RepairRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 报修申请业务实现
 *
 * @author ozj
 */
@Service
public class RepairRecordServiceImpl implements RepairRecordService {

    @Autowired
    private RepairRecordMapper repairRecordMapper;

    @Override
    @Cacheable(cacheNames = "repairApps", key = "#id", unless = "#result == null")
    public RepairApp findRepairRecordById(Integer id) {
        return repairRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    @CachePut(cacheNames = "repairApps", key = "#result.id", unless = "#result == null || #result.id == 0")
    public RepairApp createRepairRecord(RepairApp repairApp) {
        repairRecordMapper.insertSelective(repairApp);
        return repairApp;
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "repairApps", key = "#id"),
                    @CacheEvict(cacheNames = "repairAppInfo", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "allRecords", allEntries = true, beforeInvocation = true)
            }
    )
    public void deleteRepairRecordById(Integer id) {
        repairRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Caching(
            put = {
                    @CachePut(cacheNames = "repairApps", key = "#repairApp.id", unless = "#result == null")
            },
            evict = {
                    @CacheEvict(cacheNames = "repairAppInfo", allEntries = true, beforeInvocation = true),
                    @CacheEvict(cacheNames = "allRecords", allEntries = true, beforeInvocation = true)
            }
    )
    public RepairApp updateRepairRecordById(RepairApp repairApp) {
        int result = repairRecordMapper.updateByPrimaryKeySelective(repairApp);
        if (result == 0) {
            throw new RuntimeException("更新失败，更新结果数为0");
        }
        return repairApp;
    }

    @Override
    @Cacheable(cacheNames = "repairRecordInfo",
            key = "'findAlls' + #page + #pageSize + #status + #workcellId", unless = "#result == null")
    public PageInfo<RepairApp> findRepairRecords(int page, int pageSize, int status, int workcellId) {
        PageHelper.startPage(page, pageSize);
        List<RepairApp> list = repairRecordMapper.selectAll(status, workcellId);
        return new PageInfo<>(list);
    }

    @Override
    @Cacheable(cacheNames = "repairRecordInfo",
            key = "'findAllRepairRecordsOfOperatorOne' + #page + #pageSize + #status + #user.id + #user.workcell.id",
            unless = "#result == null")
    public PageInfo<RepairApp> findAllRepairRecordsOfOperatorOne(int page, int pageSize, int status, User user) {
        PageHelper.startPage(page, pageSize);
        List<RepairApp> list = repairRecordMapper.selectAllOfOperator(status, user.getId(), user.getWorkcell().getId());
        return new PageInfo<>(list);
    }

    @Override
    @Cacheable(cacheNames = "allRecords", key = "'findAllRepairRecordNums'", unless = "#result == 0")
    public int findAllRepairRecordNums() {
        return repairRecordMapper.selectAllRecord();
    }
}